<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>菜品管理 - 外卖平台</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            background: #f5f5f5;
            color: #333;
        }
        
        .header {
            background: white;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            padding: 20px 0;
            margin-bottom: 30px;
        }
        
        .header-content {
            max-width: 1200px;
            margin: 0 auto;
            padding: 0 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        
        .header h1 {
            color: #ff6b6b;
            font-size: 28px;
        }
        
        .btn {
            padding: 10px 20px;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            font-weight: 500;
            transition: all 0.3s ease;
            text-decoration: none;
            display: inline-block;
        }
        
        .btn-primary {
            background: #ff6b6b;
            color: white;
        }
        
        .btn-primary:hover {
            background: #ee5a24;
            transform: translateY(-2px);
        }
        
        .btn-secondary {
            background: #6c757d;
            color: white;
        }
        
        .btn-secondary:hover {
            background: #5a6268;
        }
        
        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 0 20px;
        }
        
        .form-section {
            background: white;
            border-radius: 12px;
            padding: 30px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            margin-bottom: 30px;
        }
        
        .form-section h2 {
            color: #333;
            margin-bottom: 20px;
            font-size: 24px;
        }
        
        .form-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
            gap: 20px;
            margin-bottom: 20px;
        }
        
        .form-group {
            margin-bottom: 20px;
        }
        
        .form-group label {
            display: block;
            margin-bottom: 8px;
            color: #333;
            font-weight: 600;
        }
        
        .form-group input,
        .form-group select,
        .form-group textarea {
            width: 100%;
            padding: 12px;
            border: 2px solid #e1e5e9;
            border-radius: 8px;
            font-size: 14px;
            transition: all 0.3s ease;
        }
        
        .form-group input:focus,
        .form-group select:focus,
        .form-group textarea:focus {
            outline: none;
            border-color: #ff6b6b;
            box-shadow: 0 0 0 3px rgba(255, 107, 107, 0.1);
        }
        
        .form-group textarea {
            height: 100px;
            resize: vertical;
        }
        
        .image-upload {
            border: 2px dashed #ddd;
            border-radius: 8px;
            padding: 20px;
            text-align: center;
            transition: all 0.3s ease;
            cursor: pointer;
        }
        
        .image-upload:hover {
            border-color: #ff6b6b;
            background: rgba(255, 107, 107, 0.05);
        }
        
        .image-upload.dragover {
            border-color: #ff6b6b;
            background: rgba(255, 107, 107, 0.1);
        }
        
        .image-preview {
            margin-top: 15px;
        }
        
        .image-preview img {
            max-width: 200px;
            max-height: 150px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        
        .gallery-preview {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
            gap: 10px;
            margin-top: 15px;
        }
        
        .gallery-item {
            position: relative;
            border-radius: 8px;
            overflow: hidden;
        }
        
        .gallery-item img {
            width: 100%;
            height: 100px;
            object-fit: cover;
        }
        
        .gallery-item .remove-btn {
            position: absolute;
            top: 5px;
            right: 5px;
            background: rgba(255, 0, 0, 0.8);
            color: white;
            border: none;
            border-radius: 50%;
            width: 25px;
            height: 25px;
            cursor: pointer;
            font-size: 12px;
        }
        
        .dishes-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
            gap: 20px;
            margin-top: 20px;
        }
        
        .dish-card {
            background: white;
            border-radius: 12px;
            padding: 20px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            transition: all 0.3s ease;
        }
        
        .dish-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 5px 20px rgba(0,0,0,0.15);
        }
        
        .dish-image {
            width: 100%;
            height: 200px;
            border-radius: 8px;
            overflow: hidden;
            margin-bottom: 15px;
        }
        
        .dish-image img {
            width: 100%;
            height: 100%;
            object-fit: cover;
        }
        
        .dish-info h3 {
            color: #333;
            margin-bottom: 10px;
            font-size: 18px;
        }
        
        .dish-price {
            color: #ff6b6b;
            font-size: 20px;
            font-weight: bold;
            margin-bottom: 10px;
        }
        
        .dish-category {
            background: #f8f9fa;
            color: #666;
            padding: 4px 8px;
            border-radius: 4px;
            font-size: 12px;
            display: inline-block;
            margin-bottom: 10px;
        }
        
        .dish-description {
            color: #666;
            font-size: 14px;
            line-height: 1.5;
            margin-bottom: 15px;
        }
        
        .dish-actions {
            display: flex;
            gap: 10px;
        }
        
        .edit-btn {
            background: #28a745;
            color: white;
            border: none;
            padding: 8px 16px;
            border-radius: 6px;
            cursor: pointer;
            font-size: 14px;
        }
        
        .delete-btn {
            background: #dc3545;
            color: white;
            border: none;
            padding: 8px 16px;
            border-radius: 6px;
            cursor: pointer;
            font-size: 14px;
        }
        
        .back-btn {
            position: fixed;
            top: 20px;
            left: 20px;
            background: rgba(255, 107, 107, 0.8);
            border: none;
            border-radius: 50%;
            width: 50px;
            height: 50px;
            cursor: pointer;
            color: white;
            font-size: 20px;
            transition: all 0.3s ease;
            z-index: 1000;
        }
        
        .back-btn:hover {
            background: #ff6b6b;
            transform: scale(1.1);
        }
        
        .hidden {
            display: none;
        }
        
        .message {
            padding: 12px;
            border-radius: 8px;
            margin-bottom: 20px;
            font-weight: 500;
            text-align: center;
        }
        
        .message.success {
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        
        .message.error {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
    </style>
</head>
<body>
    <button class="back-btn" onclick="goBack()">←</button>
    
    <div class="header">
        <div class="header-content">
            <h1>🍽️ 菜品管理</h1>
            <div>
                <button class="btn btn-primary" onclick="showAddForm()">+ 添加新菜品</button>
                <a href="main.html" class="btn btn-secondary">返回主页</a>
            </div>
        </div>
    </div>
    
    <div class="container">
        <div id="message"></div>
        
        <!-- 添加/编辑菜品表单 -->
        <div class="form-section" id="addForm">
            <h2 id="formTitle">添加新菜品</h2>
            <form onsubmit="handleSubmit(event)">
                <div class="form-grid">
                    <div class="form-group">
                        <label>菜品名称 *</label>
                        <input type="text" id="dishName" required>
                    </div>
                    <div class="form-group">
                        <label>价格 (元) *</label>
                        <input type="number" id="dishPrice" step="0.01" required>
                    </div>
                    <div class="form-group">
                        <label>原价 (元)</label>
                        <input type="number" id="dishOriginalPrice" step="0.01">
                    </div>
                    <div class="form-group">
                        <label>分类 *</label>
                        <select id="dishCategory" required>
                            <option value="">请选择分类</option>
                            <option value="川菜">川菜</option>
                            <option value="粤菜">粤菜</option>
                            <option value="湘菜">湘菜</option>
                            <option value="鲁菜">鲁菜</option>
                            <option value="苏菜">苏菜</option>
                            <option value="浙菜">浙菜</option>
                            <option value="闽菜">闽菜</option>
                            <option value="徽菜">徽菜</option>
                            <option value="快餐">快餐</option>
                            <option value="小吃">小吃</option>
                            <option value="饮品">饮品</option>
                            <option value="甜品">甜品</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label>库存数量</label>
                        <input type="number" id="dishStock" value="100">
                    </div>
                    <div class="form-group">
                        <label>制作时间 (分钟)</label>
                        <input type="number" id="dishCookingTime" value="15">
                    </div>
                </div>
                
                <div class="form-group">
                    <label>菜品描述</label>
                    <textarea id="dishDescription" placeholder="请输入菜品描述..."></textarea>
                </div>
                
                <div class="form-group">
                    <label>主要图片 *</label>
                    <div class="image-upload" onclick="document.getElementById('mainImage').click()">
                        <p>点击上传主图片</p>
                        <p style="color: #666; font-size: 12px;">支持 JPG、PNG 格式，建议尺寸 400x300</p>
                        <input type="file" id="mainImage" accept="image/*" style="display: none;" onchange="handleMainImageUpload(event)">
                    </div>
                    <div class="image-preview" id="mainImagePreview"></div>
                </div>
                
                <div class="form-group">
                    <label>菜品相册</label>
                    <div class="image-upload" onclick="document.getElementById('galleryImages').click()">
                        <p>点击上传相册图片</p>
                        <p style="color: #666; font-size: 12px;">最多上传5张图片</p>
                        <input type="file" id="galleryImages" accept="image/*" multiple style="display: none;" onchange="handleGalleryUpload(event)">
                    </div>
                    <div class="gallery-preview" id="galleryPreview"></div>
                </div>
                
                <div class="form-grid">
                    <div class="form-group">
                        <label>特色标签</label>
                        <div style="display: flex; gap: 10px; flex-wrap: wrap;">
                            <label><input type="checkbox" value="辣"> 辣</label>
                            <label><input type="checkbox" value="不辣"> 不辣</label>
                            <label><input type="checkbox" value="素食"> 素食</label>
                            <label><input type="checkbox" value="推荐"> 推荐</label>
                            <label><input type="checkbox" value="热销"> 热销</label>
                        </div>
                    </div>
                    <div class="form-group">
                        <label>主要食材</label>
                        <input type="text" id="dishIngredients" placeholder="用逗号分隔，如：鸡肉,土豆,胡萝卜">
                    </div>
                </div>
                
                <div class="form-group">
                    <label>过敏原信息</label>
                    <input type="text" id="dishAllergens" placeholder="如：花生,海鲜,牛奶等">
                </div>
                
                <div style="display: flex; gap: 15px; margin-top: 20px;">
                    <button type="submit" class="btn btn-primary">保存菜品</button>
                    <button type="button" class="btn btn-secondary" onclick="resetForm()">重置表单</button>
                    <button type="button" class="btn btn-secondary" onclick="hideAddForm()">取消</button>
                </div>
            </form>
        </div>
        
        <!-- 菜品列表 -->
        <div class="form-section">
            <h2>菜品列表</h2>
            <div class="dishes-grid" id="dishesGrid">
                <!-- 菜品将通过JavaScript动态加载 -->
            </div>
        </div>
    </div>

    <script>
        let dishes = JSON.parse(localStorage.getItem('dishes') || '[]');
        let currentEditingId = null;
        let mainImageFile = null;
        let galleryImages = [];
        
        // 页面加载时初始化
        window.onload = function() {
            loadDishes();
            hideAddForm();
        };
        
        function showMessage(text, type) {
            const messageDiv = document.getElementById('message');
            messageDiv.innerHTML = `<div class="message ${type}">${text}</div>`;
            setTimeout(() => {
                messageDiv.innerHTML = '';
            }, 3000);
        }
        
        function showAddForm() {
            document.getElementById('addForm').classList.remove('hidden');
            document.getElementById('formTitle').textContent = '添加新菜品';
            resetForm();
        }
        
        function hideAddForm() {
            document.getElementById('addForm').classList.add('hidden');
            currentEditingId = null;
        }
        
        function resetForm() {
            document.getElementById('dishName').value = '';
            document.getElementById('dishPrice').value = '';
            document.getElementById('dishOriginalPrice').value = '';
            document.getElementById('dishCategory').value = '';
            document.getElementById('dishStock').value = '100';
            document.getElementById('dishCookingTime').value = '15';
            document.getElementById('dishDescription').value = '';
            document.getElementById('dishIngredients').value = '';
            document.getElementById('dishAllergens').value = '';
            document.getElementById('mainImagePreview').innerHTML = '';
            document.getElementById('galleryPreview').innerHTML = '';
            mainImageFile = null;
            galleryImages = [];
            
            // 重置复选框
            document.querySelectorAll('input[type="checkbox"]').forEach(cb => {
                cb.checked = false;
            });
        }
        
        function handleMainImageUpload(event) {
            const file = event.target.files[0];
            if (file) {
                mainImageFile = file;
                const reader = new FileReader();
                reader.onload = function(e) {
                    document.getElementById('mainImagePreview').innerHTML = `
                        <img src="${e.target.result}" alt="主图片预览">
                        <button type="button" onclick="removeMainImage()" style="margin-top: 10px; background: #dc3545; color: white; border: none; padding: 5px 10px; border-radius: 4px; cursor: pointer;">删除图片</button>
                    `;
                };
                reader.readAsDataURL(file);
            }
        }
        
        function removeMainImage() {
            mainImageFile = null;
            document.getElementById('mainImagePreview').innerHTML = '';
            document.getElementById('mainImage').value = '';
        }
        
        function handleGalleryUpload(event) {
            const files = Array.from(event.target.files);
            if (galleryImages.length + files.length > 5) {
                showMessage('最多只能上传5张图片！', 'error');
                return;
            }
            
            files.forEach(file => {
                if (galleryImages.length < 5) {
                    galleryImages.push(file);
                    const reader = new FileReader();
                    reader.onload = function(e) {
                        const galleryPreview = document.getElementById('galleryPreview');
                        const galleryItem = document.createElement('div');
                        galleryItem.className = 'gallery-item';
                        galleryItem.innerHTML = `
                            <img src="${e.target.result}" alt="相册图片">
                            <button class="remove-btn" onclick="removeGalleryImage(${galleryImages.length - 1})">×</button>
                        `;
                        galleryPreview.appendChild(galleryItem);
                    };
                    reader.readAsDataURL(file);
                }
            });
        }
        
        function removeGalleryImage(index) {
            galleryImages.splice(index, 1);
            renderGallery();
        }
        
        function renderGallery() {
            const galleryPreview = document.getElementById('galleryPreview');
            galleryPreview.innerHTML = '';
            galleryImages.forEach((file, index) => {
                const reader = new FileReader();
                reader.onload = function(e) {
                    const galleryItem = document.createElement('div');
                    galleryItem.className = 'gallery-item';
                    galleryItem.innerHTML = `
                        <img src="${e.target.result}" alt="相册图片">
                        <button class="remove-btn" onclick="removeGalleryImage(${index})">×</button>
                    `;
                    galleryPreview.appendChild(galleryItem);
                };
                reader.readAsDataURL(file);
            });
        }
        
        function handleSubmit(event) {
            event.preventDefault();
            
            const dishData = {
                name: document.getElementById('dishName').value,
                price: parseFloat(document.getElementById('dishPrice').value),
                originalPrice: parseFloat(document.getElementById('dishOriginalPrice').value) || null,
                category: document.getElementById('dishCategory').value,
                stock: parseInt(document.getElementById('dishStock').value),
                cookingTime: parseInt(document.getElementById('dishCookingTime').value),
                description: document.getElementById('dishDescription').value,
                ingredients: document.getElementById('dishIngredients').value,
                allergens: document.getElementById('dishAllergens').value,
                tags: Array.from(document.querySelectorAll('input[type="checkbox"]:checked')).map(cb => cb.value),
                mainImage: mainImageFile ? URL.createObjectURL(mainImageFile) : null,
                gallery: galleryImages.map(file => URL.createObjectURL(file))
            };
            
            if (currentEditingId) {
                // 编辑现有菜品
                const index = dishes.findIndex(d => d.id === currentEditingId);
                if (index !== -1) {
                    dishes[index] = { ...dishes[index], ...dishData };
                    showMessage('菜品更新成功！', 'success');
                }
            } else {
                // 添加新菜品
                const newDish = {
                    id: Date.now(),
                    ...dishData,
                    createTime: new Date().toLocaleString()
                };
                dishes.push(newDish);
                showMessage('菜品添加成功！', 'success');
            }
            
            localStorage.setItem('dishes', JSON.stringify(dishes));
            loadDishes();
            hideAddForm();
        }
        
        function loadDishes() {
            const dishesGrid = document.getElementById('dishesGrid');
            
            if (dishes.length === 0) {
                dishesGrid.innerHTML = `
                    <div style="grid-column: 1 / -1; text-align: center; padding: 40px; color: #666;">
                        <p>暂无菜品，点击"添加新菜品"开始添加</p>
                    </div>
                `;
                return;
            }
            
            dishesGrid.innerHTML = dishes.map(dish => `
                <div class="dish-card">
                    <div class="dish-image">
                        <img src="${dish.mainImage || 'https://via.placeholder.com/300x200?text=菜品图片'}" alt="${dish.name}">
                    </div>
                    <div class="dish-info">
                        <h3>${dish.name}</h3>
                        <div class="dish-price">¥${dish.price}</div>
                        <div class="dish-category">${dish.category}</div>
                        <div class="dish-description">${dish.description || '暂无描述'}</div>
                        <div class="dish-actions">
                            <button class="edit-btn" onclick="editDish(${dish.id})">编辑</button>
                            <button class="delete-btn" onclick="deleteDish(${dish.id})">删除</button>
                        </div>
                    </div>
                </div>
            `).join('');
        }
        
        function editDish(id) {
            const dish = dishes.find(d => d.id === id);
            if (!dish) return;
            
            currentEditingId = id;
            document.getElementById('formTitle').textContent = '编辑菜品';
            document.getElementById('addForm').classList.remove('hidden');
            
            // 填充表单数据
            document.getElementById('dishName').value = dish.name;
            document.getElementById('dishPrice').value = dish.price;
            document.getElementById('dishOriginalPrice').value = dish.originalPrice || '';
            document.getElementById('dishCategory').value = dish.category;
            document.getElementById('dishStock').value = dish.stock;
            document.getElementById('dishCookingTime').value = dish.cookingTime;
            document.getElementById('dishDescription').value = dish.description || '';
            document.getElementById('dishIngredients').value = dish.ingredients || '';
            document.getElementById('dishAllergens').value = dish.allergens || '';
            
            // 设置标签
            document.querySelectorAll('input[type="checkbox"]').forEach(cb => {
                cb.checked = dish.tags && dish.tags.includes(cb.value);
            });
            
            // 显示现有图片
            if (dish.mainImage) {
                document.getElementById('mainImagePreview').innerHTML = `
                    <img src="${dish.mainImage}" alt="主图片预览">
                    <button type="button" onclick="removeMainImage()" style="margin-top: 10px; background: #dc3545; color: white; border: none; padding: 5px 10px; border-radius: 4px; cursor: pointer;">删除图片</button>
                `;
            }
            
            if (dish.gallery && dish.gallery.length > 0) {
                const galleryPreview = document.getElementById('galleryPreview');
                galleryPreview.innerHTML = '';
                dish.gallery.forEach((imageUrl, index) => {
                    const galleryItem = document.createElement('div');
                    galleryItem.className = 'gallery-item';
                    galleryItem.innerHTML = `
                        <img src="${imageUrl}" alt="相册图片">
                        <button class="remove-btn" onclick="removeGalleryImage(${index})">×</button>
                    `;
                    galleryPreview.appendChild(galleryItem);
                });
            }
        }
        
        function deleteDish(id) {
            if (confirm('确定要删除这个菜品吗？')) {
                dishes = dishes.filter(d => d.id !== id);
                localStorage.setItem('dishes', JSON.stringify(dishes));
                loadDishes();
                showMessage('菜品删除成功！', 'success');
            }
        }
        
        function goBack() {
            window.location.href = 'main.html';
        }
    </script>
</body>
</html>
